package org.ouwa.lms.server.service;

import org.ouwa.lms.client.model.LoginInfo;
import org.ouwa.lms.client.service.GoogleLoginService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class GoogleLoginServiceImpl extends RemoteServiceServlet implements
		GoogleLoginService {
	@Override
	public LoginInfo login(String requestUri) {
		LoginInfo loginInfo = new LoginInfo();
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		if(user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
			loginInfo.setNickname(user.getNickname());
			loginInfo.setEmailAddress(user.getEmail());
		}
		else {
			loginInfo.setLoggedIn(false);
		}
		loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		
		return loginInfo;
	}
}
